<template>
  <common-search :show-item="2" :col-layout="{xxl: 6, xl:8, lg: 8, md: 12, sm: 24, xs: 24}" v-model="query" :formItems="formItems" @search="search" @reset="reset"></common-search>
  <a-button type="primary" @click="exports" :loading="exportLoading" v-modulePerm="['warehouseConfiguration:export']">
    <template #icon>
      <ExportOutlined />
    </template>
    导出
  </a-button>
  <a-button style="margin-left: 10px;" type="primary" @click="handleSync" :loading="syncLoading" v-modulePerm="['warehouseConfiguration:sync']">
    同步易仓
  </a-button>
<!--  <a-button type="primary" @click="openSuperSearch = true" style="margin-left: 20px;">-->
<!--    <template #icon>-->
<!--      <HeartFilled />-->
<!--    </template>-->
<!--    高级查询-->
<!--  </a-button>-->
  <my-alert @onClearSelected="onClearSelected" :selectedRowKeys="selectedRowKeys" style="margin-top: 10px;">
    <template #setting>
      <columns-check-box :columns="columns" :name="'checkedList'" v-model:checkedList="checkedList" v-model:filter-columns="filterColumns"/>
    </template>
  </my-alert>
  <common-table
      :customFilterDropdown="true"
      :columns="filterColumns"
      :dataSource="dataList"
      v-model:localPagination="page"
      @paginationChange="onPageChange"
      v-model:loading="loading"
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :rowKey="(record) => record.id"
      :scroll="{ x: 'max-content'}"
      :slots="['action','addressItem','serviceItem']"
  >
    <template #action="{ record }">
      <a  @click="handleEdit(record)" v-modulePerm="['warehouseConfiguration:edit']">编辑</a>
      <a-divider type="vertical" />
      <a  @click="handleDetail(record)">详情</a>
    </template>
    <template #addressItem="{ record }">
      {{record.addressItem.addressLine1 + ' ' + record.addressItem.addressLine2}}
    </template>
    <template #serviceItem="{ record }">
      {{(record.serviceItem.warehouseService) + ' ' + (record.serviceItem.serviceCode != null ?  record.serviceItem.serviceCode : '')}}
    </template>
  </common-table>
  <super-search :columns="columns" :openSuperSearch="openSuperSearch" @close="closeSuperSearch"/>
  <editModal :openEdit="openEdit" :warehouseCode="warehouseCode" @update="search(page.pageNo)" @close="handleEditClose"/>
  <detailModal :openDetail="openDetail" :warehouseCode="warehouseCode" @close="handleDetailClose"/>
</template>
<script>
export default {
  name:'basicInformation-warehouseConfiguration'
}
</script>
<script setup>
import CommonSearch from "@/components/commonSearch/index.vue";
import CommonTable from "@/components/commonTable/index.vue";
import {useSearchTable} from "@/hooks/table/useSearchTable";
import {ref,onMounted} from "vue";
import {message} from "ant-design-vue";
import {postFile} from "@utils/service/index.js";
import {getManyDictItems} from "@/api/system/dict.js";
import SuperSearch from "@/components/modal/superSearch.vue";
import {getWarehousePage, syncWarehouse} from "@/api/basicInformation/warehouseConfiguration.js";
import MyAlert from "@/components/alert/my-alert.vue";
import ColumnsCheckBox from "@/components/popconfirm/columnsCheckBox.vue";
import {getLVal} from "@utils/storage.js";
import editModal from "@/views/basicInformation/components/editModal.vue";
import detailModal from "@/views/basicInformation/components/detailModal.vue";
const { dataList, query, loading, page, search, reset, onPageChange } = useSearchTable({
  searchAPI: getWarehousePage,
  queryFactory: () => ({
    warehouseStatus:'1',
    columns:'id',
    order: "desc"
  }),
});
const columns = ref([
  {
    "dataIndex": "warehouseCode",
    "title": "物流仓库编码",
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  {
    "dataIndex": "warehouseName",
    "title": "物流仓库名称",
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  {
    "dataIndex": "warehouseStatus_dictText",
    dict:'warehouse_status',
    "title": "物流仓库状态",
    "align": "center",
    "fieldType": "String",
    type:'select',
  },
  {
    "dataIndex": "warehouseType_dictText",
    "title": "物流仓库类型",
    dict:'warehouse_data_type',
    "align": "center",
    "fieldType": "String",
    "type":'select',
  },
  {
    "dataIndex": "site",
    "dict": "site",
    "title": "站点",
    "align": "center",
    "fieldType": "String",
    type:'select',
  },
  {
    "dataIndex": "saleZone_dictText",
    "title": "销售区域",
    dict: "sale_zone",
    "align": "center",
    "fieldType": "String",
    type:'select',
  },
  // {
  //   "dataIndex": "updateTime",
  //   "title": "更新时间",
  //   "align": "center",
  //   "fieldType": "String",
  //   "type":'date',
  // },
  {
    "dataIndex": "warehouseNameEn",
    "title": "仓库英文名称",
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  {
    "dataIndex": "warehouseCountry",
    "title": "国家",
    dict: 'country',
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  {
    "dataIndex": "zipCode",
    "title": "邮编",
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  {
    "dataIndex": "stateCode",
    "title": "州编码",
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  {
    "dataIndex": "cityName",
    "title": "城市名",
    "align": "center",
    "fieldType": "Long",
    "type":'input',
  },
  {
    "dataIndex": "addressItem",
    "title": "地址",
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  // {
  //   "dataIndex": "addressType",
  //   "title": "地址类型",
  //   "align": "center",
  //   "fieldType": "String",
  //   "type":'input',
  // },
  {
    "dataIndex": "contactPerson",
    "title": "联系人",
    "align": "center",
    "fieldType": "Date",
    "type":'input',
  },
  {
    "dataIndex": "contactPhone",
    "title": "联系电话",
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  {
    "dataIndex": "contactEmail",
    "title": "联系邮件",
    "align": "center",
    "fieldType": "Date",
    "type":'input',
  },
  {
    "dataIndex": "serviceItem",
    "title": "服务商",
    "align": "center",
    "fieldType": "String",
    type:'input'
  },
  {
    "dataIndex": "memo",
    "title": "备注",
    "align": "center",
    "fieldType": "String",
    "type":'input',
  },
  {
    dataIndex: 'action',
    title: '操作',
    align: 'center',
    fixed: 'right',
  }
])
const checkedList = ref(getLVal('checkedList') ? getLVal('checkedList') : columns.value.map(item => item.title))
const filterColumns = ref(columns.value.filter(item => checkedList.value.includes(item.title)))
const formItems = ref([
  {
    label:'编码',
    type:'input',
    field:'warehouseCode',
  },
  {
    label:'站点',
    type:'select',
    field:'site',
    options: [],
  },
  {
    label:"物流仓库类型",
    type:'select',
    field: 'warehouseType',
    options:[],

  },
  {
    label:'国家',
    type:'select',
    field:'warehouseCountry',
    options:[]
  },
  {
    label:"物流仓库状态",
    type:'select',
    field: 'warehouseStatus',
    options:[],

  },
  {
    label:'物流仓库名称',
    type:'input',
    field:'warehouseName',
  },
  {
    label:'邮编',
    type:'input',
    field:'zipCode',
  },
])
const openEdit = ref(false)
const openDetail = ref(false)
const selectedRowKeys = ref([])
const onSelectChange = (e) => {
  selectedRowKeys.value = e
}
const warehouseCode = ref('')
const exportLoading = ref(false);
const syncLoading = ref(false);
const openSuperSearch = ref(false)
const exports = async () => {
  try{
    exportLoading.value = true;
    let obj = query.value
    if(selectedRowKeys.value.length > 0){
      obj.ids = [...selectedRowKeys.value]
    }
    const res = await postFile('/wms/warehouse/export','post',obj,'仓库配置.xlsx')
    message.success('导出成功')
  }catch (e) {
    message.error(e);
  }finally {
    exportLoading.value = false;
  }
}
const handleSync = async () => {
  try{
    syncLoading.value = true;
    const res = await syncWarehouse({})
    message.success(res.message)
  }catch (e){
    console.log(e)
    message.error(e)
  }finally{
    syncLoading.value = false;
  }
}
/**
 * 初始化字典数据
 * @returns {Promise<void>}
 */
const init = async () => {
  try{
    const res = await getManyDictItems({
      dictCodeList:"warehouse_status,warehouse_data_type,country,site,sale_zone"
    })
    formItems.value[1].options = res.result.site
    formItems.value[2].options = res.result.warehouse_data_type
    formItems.value[3].options = res.result.country
    formItems.value[4].options = res.result.warehouse_status
  }catch (e){
    console.log(e)
  }
}
/**
 * 关闭高级查询
 */
const closeSuperSearch = () => {
  openSuperSearch.value = false
}
const onClearSelected = () => {
  selectedRowKeys.value = []
}
const handleEdit = (record) => {
  warehouseCode.value = record.warehouseCode
  openEdit.value = true
}
const handleEditClose = () => {
  openEdit.value = false
}
const handleDetail = (record) => {
  warehouseCode.value = record.warehouseCode
  openDetail.value = true
}
const handleDetailClose = () => {
  openDetail.value = false
}
onMounted(() => {
  init()
})
</script>
<style scoped>

</style>
